//
//  MZLoadingView.swift
//  MZTool
//
//  Created by gby on 2023/4/25.
//

import UIKit

public enum MZLoadingViewType {
    case system_light
    case system_dark
    case custom
}

public class MZView_Loading: UIView {
    
    public var type: MZLoadingViewType = .system_light
    private lazy var indicatorv: UIActivityIndicatorView = UIActivityIndicatorView.init(style: .medium)
    private lazy var customImgName: String = ""
    private lazy var customLoading: UIImageView = UIImageView.init(image: MZ.IMG(customImgName))
    //默认 size = CGPoint(x: self.bounds.width/2, y: self.bounds.height/2)
    open var loadingSize: CGSize? {
        didSet{
            if let lsize = loadingSize{
                switch type {
                case .custom:
                    customLoading.mz.size = lsize
                    customLoading.center = CGPoint(x: self.bounds.width/2, y: self.bounds.height/2)
                default:
                    break
                }
                
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    public override init(frame: CGRect) {
        super.init(frame: frame)
        create_UI()
    }
    
    public convenience init(frame: CGRect,loadType t: MZLoadingViewType, _ imgName: String = "") {
        self.init()
        self.frame = frame
        self.type = t
        if imgName.count > 0 {
            self.customImgName = imgName
        }
        create_UI()
    }
    
    private func create_UI(){
        while self.subviews.count > 0 {
            self.subviews.last?.removeFromSuperview()
        }
        switch type {
        case .system_light, .system_dark:
            self.addSubview(indicatorv)
            indicatorv.center = CGPoint(x: self.bounds.width/2, y: self.bounds.height/2)
            indicatorv.overrideUserInterfaceStyle = type == .system_light ? .dark : .light
        case .custom:
            create_Custom_Loading()
            self.addSubview(customLoading)
        }
    }
    
    public override func draw(_ rect: CGRect) {
        switch type {
        case .system_light, .system_dark:
            indicatorv.center = CGPoint(x: self.bounds.width/2, y: self.bounds.height/2)
        case .custom:
            customLoading.center = CGPoint(x: self.bounds.width/2, y: self.bounds.height/2)
        }
        super.draw(rect)
    }
    
    public func startAnimation(){
        self.superview?.bringSubviewToFront(self)
        self.isHidden = false
        switch type {
        case .system_light, .system_dark:
            indicatorv.startAnimating()
        case .custom:
            create_Custom_Loading()
        }
    }
    
    public func stopAnimation(){
        self.isHidden = true
        switch type {
        case .system_light, .system_dark:
            indicatorv.stopAnimating()
        case .custom:
            customLoading.layer.removeAllAnimations()
        }
    }
    
    private func create_Custom_Loading() {
        if let lsize = loadingSize{
            customLoading.frame = CGRect.init(x: 0, y: 0, width: lsize.width, height: lsize.height)
        }else{
            customLoading.frame = CGRect.init(x: 0, y: 0, width: self.bounds.width/2, height: self.bounds.height/2)
        }
        let animation = CABasicAnimation.init(keyPath: "transform")
        animation.fromValue = NSValue.init(caTransform3D: CATransform3DIdentity)
        animation.toValue = NSValue.init(caTransform3D: CATransform3DMakeRotation(CGFloat(Double.pi/2), 0.0, 0.0, 1.0))
        animation.duration = 0.3
        animation.isCumulative = true
        animation.repeatCount = MAXFLOAT
        animation.isRemovedOnCompletion = false
        customLoading.layer.add(animation, forKey: "animationimgv")
        customLoading.center = CGPoint(x: self.bounds.width/2, y: self.bounds.height/2)
    }
}
